import { createSlice } from "@reduxjs/toolkit"
import axios from "axios"
const foodsStore = createSlice({
  name:'foods',
  initialState:{
    foodsList:[],
    activeIndex:0
  },
  reducers:{
    setFoodsList(state,action) {
      state.foodsList = action.payload
    },
    setActiveIndex(state,action) {
      state.activeIndex = action.payload
    }
  }
})

const {setFoodsList,setActiveIndex} = foodsStore.actions
// 异步请求列表数据
const getFoodsListAsync = () => {
  return async(dispatch) => {
    const res = await axios.get('http://localhost:3004/takeaway')
    dispatch(setFoodsList(res.data))
    // console.log(res.data,'----------')
  }
}
export {getFoodsListAsync,setActiveIndex}
const reducers = foodsStore.reducer
export default reducers